// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.h"
#include <EngineUtils.h>

AMyPawn::AMyPawn()
{
 	PrimaryActorTick.bCanEverTick = true;
	_selectedActor = NULL;

	_box = CreateDefaultSubobject<UBoxComponent>(TEXT("PawnBox"));
	SetRootComponent(_box);
}

void AMyPawn::BeginPlay()
{
	Super::BeginPlay();

	APlayerController* playerCtrl = Cast<APlayerController>(Controller);
	playerCtrl->bShowMouseCursor = true;
	playerCtrl->bEnableClickEvents = true;
	playerCtrl->bEnableMouseOverEvents = true;
}

void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AMyPawn::SetupPlayerInputComponent(UInputComponent* inputComponent)
{
	Super::SetupPlayerInputComponent(inputComponent);

	inputComponent->BindAction("Click", IE_Pressed, this, &AMyPawn::OnClick);
	inputComponent->BindAxis("MoveL", this, &AMyPawn::OnMoveLeft);
	inputComponent->BindAxis("MoveR", this, &AMyPawn::OnMoveRight);
	inputComponent->BindAxis("MoveD", this, &AMyPawn::OnMoveDown);
	inputComponent->BindAxis("MoveU", this, &AMyPawn::OnMoveUp);
	inputComponent->BindVectorAxis(EKeys::Mouse2D, this, &AMyPawn::OnMouseMove);
}

void AMyPawn::OnClick()
{
	APlayerController* playerCtrl = Cast<APlayerController>(Controller);

	double mx = 0.0, my = 0.0;
	playerCtrl->GetMousePosition(mx, my);
	UE_LOG(LogTemp, Warning, TEXT("Mouse: %f, %f"), mx, my);

	FVector start, dir;
	playerCtrl->DeprojectMousePositionToWorld(start, dir);
	FlushPersistentDebugLines(GetWorld());

	FHitResult hit;
	FCollisionQueryParams params(FName(TEXT("")), false, GetOwner());
	if (GetWorld()->LineTraceSingleByObjectType(
		hit, start, start + dir * 1000.0, FCollisionObjectQueryParams(), params))
	{
		AActor* actor = hit.GetActor();
		UStaticMeshComponent* mesh = Cast<UStaticMeshComponent>(
			actor->GetComponentByClass(UStaticMeshComponent::StaticClass()));
		if (mesh && mesh->Mobility)
		{
			_selectedActor = actor; ShowSelectedActorBox();
			UE_LOG(LogTemp, Warning, TEXT("Hit: %s"), *(_selectedActor->GetName()));
		}
		else
		{
			_selectedActor = NULL;
			UE_LOG(LogTemp, Warning, TEXT("No Hits"));
		}
	}
	else
		_selectedActor = NULL;
}

void AMyPawn::ShowSelectedActorBox()
{
	FBox bbox = _selectedActor->GetComponentsBoundingBox();
	FlushPersistentDebugLines(GetWorld());
	DrawDebugBox(GetWorld(), bbox.GetCenter(), bbox.GetExtent(), FColor::Yellow, true);
}

void AMyPawn::OnMoveLeft(float v)
{
	if (!_selectedActor || v == 0.0f) return;
	_selectedActor->AddActorWorldOffset(FVector(-v, 0.0, 0.0));
	ShowSelectedActorBox();
}

void AMyPawn::OnMoveRight(float v)
{
	if (!_selectedActor || v == 0.0f) return;
	_selectedActor->AddActorWorldOffset(FVector(v, 0.0, 0.0));
	ShowSelectedActorBox();
}

void AMyPawn::OnMoveDown(float v)
{
	if (!_selectedActor || v == 0.0f) return;
	_selectedActor->AddActorWorldOffset(FVector(0.0, 0.0, -v));
	ShowSelectedActorBox();
}

void AMyPawn::OnMoveUp(float v)
{
	if (!_selectedActor || v == 0.0f) return;
	_selectedActor->AddActorWorldOffset(FVector(0.0, 0.0, v));
	ShowSelectedActorBox();
}

void AMyPawn::OnMouseMove(FVector v)
{
	AddControllerYawInput(v.X);
	AddControllerPitchInput(-v.Y);
}